package util;

import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

import model.BacSi;
import model.BaiViet;
import model.Benh;
import model.CauHoiTrucTuyen;
import model.ChuDe;
import model.ChuDeKhac;
import model.DiaDiemYTe;
import model.GenerallyObject;
import model.NguoiDung;
import model.NhomNguoiDung;
import model.ThietBiYTe;
import model.Thuoc;
import model.YKienBanDoc;


public class XMLParserServerUtil {
	public static JAXBContext jaxbContext = initJAXBContext();
	public static Marshaller marshaller;
	public static StringWriter stringWriter;
	
	public static JAXBContext initJAXBContext(){
		try {
			return jaxbContext = JAXBContext.newInstance(ListObject.class, CauHoiTrucTuyen.class, BaiViet.class,
					Benh.class, Thuoc.class, DiaDiemYTe.class, BacSi.class, ThietBiYTe.class, ChuDeKhac.class, GenerallyObject.class, YKienBanDoc.class,NguoiDung.class, NhomNguoiDung.class);
		} catch (JAXBException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static String marshalObject(Object obj){
		String result = "";
		if (obj == null)
			return "";
		else {
			try {
				marshaller = jaxbContext.createMarshaller();
				marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
				stringWriter = new StringWriter();
				marshaller.marshal(obj, stringWriter);
				result = stringWriter.toString();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return result;
	}
	
	public static String marshalListObject(ListObject<Object> list){
		String result = "";
		try {
			marshaller = jaxbContext.createMarshaller();
			marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
			stringWriter = new StringWriter();
			marshaller.marshal(list, stringWriter);
			result = stringWriter.toString();
			stringWriter.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	public static String marshalChuDe(ChuDe chuDe){
		if (chuDe == null)
			return "";
		else {
			Object obj = null;
			if (chuDe.getChuDeCha().getLoai().equals(ChuDe.BENH))
				obj = (Benh)chuDe;
			else if (chuDe.getChuDeCha().getLoai().equals(ChuDe.THUOC))
				obj = (Thuoc)chuDe;
			else if (chuDe.getChuDeCha().getLoai().equals(ChuDe.DIA_DIEM_Y_TE))
				obj = (DiaDiemYTe)chuDe;
			else if (chuDe.getChuDeCha().getLoai().equals(ChuDe.BAC_SI))
				obj = (BacSi)chuDe;
			else if (chuDe.getChuDeCha().getLoai().equals(ChuDe.THIET_BI_Y_TE))
				obj = (ThietBiYTe)chuDe;
			else if (chuDe.getChuDeCha().getLoai().equals(ChuDe.KHAC))
				obj = (ChuDeKhac)chuDe;
			else
				obj = chuDe;
			
			return marshalObject(obj);
		}
	}
	
	@SuppressWarnings("unchecked")
	public static void main(String[] args) throws JAXBException {
		jaxbContext = JAXBContext.newInstance(ListObject.class, CauHoiTrucTuyen.class);
		NhomNguoiDung nhom1 = new NhomNguoiDung("ADMIN", "Admin");
		NhomNguoiDung nhom2 = new NhomNguoiDung("MEMBER", "Member");
		NguoiDung nd1 = new NguoiDung("a", "a", nhom1, "a", "a");
		NguoiDung nd2 = new NguoiDung("b", "b", nhom2, "b", "b");
		List<NguoiDung> list = new ArrayList<NguoiDung>();
		list.add(nd1);
		list.add(nd2);
		
		ChuDe chuDe1 = new ChuDe("Benh", null, "Benh");
		CauHoiTrucTuyen cauHoi1 = new CauHoiTrucTuyen(chuDe1, "zz", "xx", nd1, new Date(), "", nd2, new Date());
		
		ListObject<Object> listObj = new ListObject(list);
		
		System.out.println(marshalListObject(listObj));
		System.out.println(marshalObject(cauHoi1));
	}
}
